package com.cts.rt.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * This class is used to represent an address with address, city, province and
 * postal-code information.
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Entity
@Table(name = "ADDRESS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "OWNER", discriminatorType = DiscriminatorType.STRING)
public class Address extends BaseObject implements Serializable {
	private static final long serialVersionUID = 3617859655330969141L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	public Long id;

	@Column(name = "ADDRESS", length = 150)
	private String address;

	@Column(name = "CITY", length = 50)
	private String city;

	@Column(name = "PROVINCE", length = 100)
	private String province;

	@Column(name = "COUNTRY", length = 100)
	private String country;

	@Column(name = "POSTAL_CODE", length = 15)
	private String postalCode;

	@Column(name = "TYPE", nullable = false)
	private String type;

	public String getAddress() {
		return address;
	}

	public String getCity() {
		return city;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getProvince() {
		return province;
	}

	public String getCountry() {
		return country;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	/**
	 * Overridden equals method for object comparison. Compares based on
	 * hashCode.
	 * 
	 * @param o
	 *            Object to compare
	 * @return true/false based on hashCode
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Address)) {
			return false;
		}

		final Address address1 = (Address) o;

		return this.hashCode() == address1.hashCode();
	}

	/**
	 * Overridden hashCode method - compares on address, city, province, country
	 * and postal code.
	 * 
	 * @return hashCode
	 */
	public int hashCode() {
		int result;
		result = (address != null ? address.hashCode() : 0);
		result = 29 * result + (city != null ? city.hashCode() : 0);
		result = 29 * result + (province != null ? province.hashCode() : 0);
		result = 29 * result + (country != null ? country.hashCode() : 0);
		result = 29 * result + (postalCode != null ? postalCode.hashCode() : 0);
		return result;
	}

	/**
	 * Returns a multi-line String with key=value pairs.
	 * 
	 * @return a String representation of this class.
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append("country", this.country)
				.append("address", this.address)
				.append("province", this.province)
				.append("postalCode", this.postalCode)
				.append("city", this.city).toString();
	}
}
